
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.Timer;

import sun.audio.AudioData;
import sun.audio.AudioDataStream;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Riso
 */
public class Strela extends JLabel implements ActionListener {

    Panel j;
    int x;
    int y;
    Timer t = new Timer(100, this);
    public static int strely = 0;
//    ImageIcon strela = new ImageIcon("img/strela.png");
    ImageIcon strela = new javax.swing.ImageIcon(getClass().getResource("/img/strela.png"));
    Rectangle rectangle;

    public Strela() {
    }

    public void hudba() {
        AudioPlayer ap = AudioPlayer.player;
        AudioStream as;
        AudioData ad;
        AudioDataStream zvuk = null;
        try {
            as = new AudioStream(new FileInputStream("./zvuky/vystrel_l.wav"));
            ad = as.getData();
            zvuk = new AudioDataStream(ad);
        } catch (Exception e) {
        }
        ap.start(zvuk);
    }

    public Strela(Panel j, int x, int y) {
        if (strely < 5) {
            this.j = j;
            setBounds(x, y, 4, 8);
            this.setIcon(strela);
            this.rectangle = new Rectangle(x, y, 4, 8);
            t.start();
            strely++;
            hudba();
        }
    }

    public int pocetstriel() {
        return strely;
    }

    public Rectangle getRectangle() {
        return rectangle;
    }

    public void zmizni() {
        t.stop();
        j.remove(this);
        strely--;
        rectangle.setLocation(600, 600);
    }

    public void actionPerformed(ActionEvent e) {
        setLocation(getX(), getY() + 10);
        rectangle.setLocation(getX(), getY());
        if (this.getY() > 300) {
            j.remove(this);
            t.stop();
            strely--;
            updateUI();
        }
    }
}
